<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="砍价状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择砍价状态" clearable size="small">
          <el-option label="进行中" :value="1" />
          <el-option label="未完成" :value="2" />
          <el-option label="已成功" :value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="开启时间" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          :default-time="['00:00:00', '23:59:59']"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
                   v-hasPermi="['store:bargain-user:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['store:bargain-user:export']">导出</el-button>
      </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

            <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
            <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="头像" align="center">
        <template v-slot="scope">
          <el-avatar :src="scope.row.avatar" size="medium" style="width:48px;height:48px;" />
        </template>
      </el-table-column>
      <el-table-column label="发起用户" align="center" prop="nickname" />
      <el-table-column label="开启时间" align="center" prop="startTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="砍价商品" align="center" prop="title" width="260">
        <template v-slot="scope">
          <el-tooltip effect="light" placement="right">
            <div slot="content" style="background:#fff;padding:16px;max-width:320px;border-radius:8px;box-shadow:0 2px 12px 0 rgba(0,0,0,0.1);word-break:break-all;line-height:1.7;">
              {{ scope.row.title }}
            </div>
            <span style="display:inline-block;max-width:240px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">{{ scope.row.title }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="最低价" align="center" prop="bargainPriceMin" />
      <el-table-column label="当前价" align="center" prop="bargainPrice" />
      <el-table-column label="总砍价次数" align="center" prop="bargainNum" width="120" />
      <el-table-column label="剩余砍价次数" align="center" prop="myBargainCount" width="120" />
      <el-table-column label="结束时间" align="center" prop="stopTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.stopTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="砍价状态" align="center" prop="status">
        <template v-slot="scope">
          <el-button :type="scope.row.status === 1 ? 'warning' : (scope.row.status === 2 ? 'danger' : (scope.row.status === 3 ? 'success' : 'info'))" size="mini" plain>
            {{ scope.row.status === 1 ? '进行中' : (scope.row.status === 2 ? '未完成' : (scope.row.status === 3 ? '已成功' : '未知')) }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="openForm(scope.row.id)">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <!-- 对话框(添加 / 修改) -->
    <BargainUserForm ref="formRef" @success="getList" />
    </div>
</template>

<script>
import * as BargainUserApi from '@/api/store/bargainuser';
import BargainUserForm from './BargainUserForm.vue';
import { parseTime } from '@/utils/index';
export default {
  name: "BargainUser",
  components: {
          BargainUserForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
              // 总条数
        total: 0,
      // 砍价列表列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        status: null,
        timeRange: []
      },
            };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const params = {
          ...this.queryParams,
          startTime: this.queryParams.timeRange && this.queryParams.timeRange[0] ? new Date(this.queryParams.timeRange[0]).getTime() : undefined,
          endTime: this.queryParams.timeRange && this.queryParams.timeRange[1] ? new Date(this.queryParams.timeRange[1]).getTime() : undefined,
        };
        delete params.timeRange;
        const res = await BargainUserApi.getBargainUserPage(params);
        this.list = res.data.list;
        this.total = res.data.total || 0;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除砍价列表编号为"' + id + '"的数据项?')
      try {
       await BargainUserApi.deleteBargainUser(id);
       await this.getList();
       this.$modal.msgSuccess("删除成功");
      } catch {}
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有砍价列表数据项?');
      try {
        this.exportLoading = true;
        const data = await BargainUserApi.exportBargainUserExcel(this.queryParams);
        this.$download.excel(data, '砍价列表.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
              }
};
</script>
